package com.clkj.zhonghui.service.stored;

import com.clkj.zhonghui.dao.stored.ShareMapper;
import com.clkj.zhonghui.pojo.common.OSSUtil;
import com.clkj.zhonghui.pojo.product.Category;
import com.clkj.zhonghui.pojo.stored.ShareImg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：周佳润
 * @Date：2024/7/18 上午10:46
 */
@Service
public class ShareServiceImpl implements ShareService{

    @Autowired
    private ShareMapper shareMapper;

    @Override
    public void addShare(ShareImg shareImg, String flag, HttpServletRequest request) {
        MultipartFile file = ((MultipartRequest) request).getFile("fileShare");
        MultipartFile wexinFile = ((MultipartRequest) request).getFile("fileWeiXin");
        String fileName = "";
        if (file != null) {
            fileName = OSSUtil.uploadingImg(file,"zhonghui/share");
        }
        if (wexinFile != null) {
            fileName = OSSUtil.uploadingImg(wexinFile,"zhonghui/weixin");
        }
        if(fileName!=""){
            shareImg.setImgName(fileName);
        }
        if("add".equals(flag)){
            shareMapper.insertSelective(shareImg);
        }else{
            shareMapper.updateByPrimaryKeySelective(shareImg);
        }
    }

    @Override
    public PageInfo listShare(Integer page, Integer size, String imgFlag) {
        PageHelper.startPage(page,size);
        Example example = new Example(ShareImg.class);
        Example.Criteria criteria = example.createCriteria();
        if (imgFlag != null && !imgFlag.equals(-1)) {
            criteria.andEqualTo("imgFlag", imgFlag);
        }
        return new PageInfo(shareMapper.selectByExample(example)) ;
    }

    @Override
    public void delShare(String cateIds) {
        Example example = new Example(ShareImg.class);
        Example.Criteria criteria = example.createCriteria();
        String id [] = cateIds.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String s : id) {
            idList.add(Integer.parseInt(s));
        }
        criteria.andIn("imgId",idList);
        shareMapper.deleteByExample(example);
    }
}
